<template>
	<view>
		<view class="current">
			<view class="adds">
				<image src="../../static/icon/addr.png"></image>
				<view>当前位置：</view>
				<view class="currSeat">成都</view>
			</view>
			<view class="gps">
				<image src="../../static/icon/addr2.png"></image>
				<view>重新定位</view>
			</view>
		</view>
		<view class="seat">
			<view>
				<view v-for="(item,index) in provinceData" class="hot_seat" @click="province(item.value,index)"
					:key="index" :class="{'callUp':index == prosIndex}">
					{{item.label}}
				</view>
			</view>
			<view v-show="cityShow">
				<view @click="allPro(allPros[0].value)">
					全{{allPros[0].label}}
				</view>
				<view class="area" v-for="(item,index) in newCity" :key="index" @click="citys(item.value,index)"
						:class="{'callUp':index == cityIndex}">
					{{item.label}}
				</view>
			</view>
			<view>
				<view v-show="areaShow" v-for="(item,index) in newArea" :key="index" @click="areaCli(item.value)">
					{{item.label}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				provinceData: [],
				allPros: [],
				// 市
				cityData: [],
				newCity: [],
				// 区
				areaData: [],
				newArea: [],
				cityShow: false,
				areaShow: false,
				prosIndex: '-1',
				cityIndex: '-1'
			}
		},
		onLoad() {
			const app = getApp()
			app.getData('addons/shopro/address/area', {}).then(res => {
				// console.log(res.data.data)
				this.provinceData = res.data.data.provinceData
				this.cityData = res.data.data.cityData
				this.areaData = res.data.data.areaData
				// console.log(this.provinceData)
				console.log(this.cityData)
				// console.log(this.areaData)
			})
		},
		methods: {
			province(id, index) {
				console.log(index)
				this.prosIndex = index
				var city = this.cityData
				var pro = this.provinceData
				var cityPid = []
				var allPro = []
				city.forEach(item => {
					// console.log(item)
					var cityItem = item
					cityItem.forEach(item => {
						// console.log(item)
						// 对比pid和value获取对应省份的市
						if (id == item.pid) {
							// console.log(item)
							cityPid.push(item)
							var pids = item.pid
							// 根据当前id获取对应省份显示全xx省
							pro.forEach(item => {
								if (item.value == pids) {
									// console.log(item,"全省")
									allPro.push(item)
								}
							})
						}
					})
				})
				// 全XX省的值
				this.allPros = allPro
				// 省份对应的市区
				this.newCity = cityPid
				console.log(this.allPros, '全省')
				// 控制二三级列表的显示和隐藏
				this.cityShow = true
				// this.areaShow = false
				this.newArea = [{
					label: '请选择市'
				}]
			},
			citys(id,index) {
				console.log(index)
				this.cityIndex = index
				var area = this.areaData
				var areaPid = []
				area.forEach(item => {
					// console.log(item)
					var areaItem = item
					areaItem.forEach(item => {
						// console.log(item,'第二次循环')
						var areaTwo = item
						areaTwo.forEach(item => {
							// console.log(item,'第三次循环')
							if (id == item.pid) {
								// console.log(item,'这是获取的区')
								areaPid.push(item)
							}
						})

					})
				})
				this.newArea = areaPid
				this.areaShow = true
				console.log(this.newArea, '这是获取的区')
			},
			// 点击全XX市跳转主页并传值
			allPro(id) {
				console.log(id)
				var pro = this.provinceData
				var areaArr = []
				// console.log(this.provinceData,'这个是市')
				pro.forEach(item => {
					console.log(item)
					if(id == item.value) {
						let areaPro = {
							label: item.label,
							value: item.value
						}
						areaArr.push(areaPro)
					}
					
					
				})
				app.globalData.areas = areaArr
				uni.switchTab({
					url: '/pages/home/home?'
				})
			},
			// 点击区跳转主页并传值
			areaCli(id) {
				console.log(id)
				var arrArea = this.newArea
				var areaArr = []
				arrArea.forEach(item => {
					// console.log(item,'11111111111111')
					if(id == item.value) {
						let areaUp = {
							label: item.label,
							value: item.value
						}
						areaArr.push(areaUp)
					}
				})
				app.globalData.areas = areaArr
				uni.switchTab({
					url: '/pages/home/home?'
				})
			}
		}
	}
</script>

<style lang="scss">
	.current {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.current image {
		width: 30rpx;
		height: 30rpx;
	}
	.gps image {
		width: 50rpx;
		height: 50rpx;
	}
	.adds, .gps {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 28rpx;
		padding: 10rpx;
	}
	.currSeat, .gps {
		color: #1e9efe;
	}
	.seat {
		display: flex;
		justify-content: space-between;
		background-color: #f2f2f2;
	}

	.seat>view {
		width: 33%;
	}

	.seat>view>view {
		height: 60rpx;
		background-color: #FFF;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.callUp {
		color: #1e9efe;
	}
</style>
